package tom.spring.cloud.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author ZHUFEIFEI
 */
@Slf4j
@EnableCircuitBreaker //启用熔断
@EnableDiscoveryClient //启用服务注册发现客户端
@SpringBootApplication
@ServletComponentScan //扫描servlet/filter/listener等组件,只在使用内置web服务器生效
public class Consumer01Application {
    public static void main(String[] args) {
        SpringApplication.run(Consumer01Application.class, args);
    }

    /**
     * LoadBalanced注解为restTemplate启用Ribbon客户端负载均衡
     * @param builder
     * @return
     */
    @Bean
    @LoadBalanced
    @ConditionalOnMissingBean(RestTemplate.class)
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        log.info("build restTemplate using RestTemplateBuilder.");
        return builder.build();
    }

    /**
     * hystrix.stream 配置management.endpoints.web.exposure.include包含即可不需要手动引入
     * @return
     */
//    @Bean
//    public ServletRegistrationBean registerHystrixStreamServlet() {
//        ServletRegistrationBean hystrix = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
//        hystrix.setLoadOnStartup(1);
//        hystrix.addUrlMappings("/hystrix.stream");
//        hystrix.setName("HystrixMetricsStreamServlet");
//        return hystrix;
//    }
}
